﻿/// <reference path="libraries/jQuery.js" />

var hasUserName = false;
function Indicacao() {
    this.VagaId = 0;
    this.UsuarioIndicadoId = 0;
    this.Status = 0;
    this.DataCadastro = null;
    var url = eS.UrlPadrao + 'Views/cadastros/indicacao.aspx/';
    var ExecuteMethod = function _ExecuteMethod(url, parans, callback) {
        //$('div[id="membros"]').block({ message: 'aguarde <img src="' + eS.UrlPadrao + '/recursos/i/loading.gif" />' });
        $.ajax({
            url: url,
            type: 'post',
            data: parans,
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: callback,
            error: function (e) {
                var _alert = new Alert();
                _alert.Name = "erroGeral";
                _alert.Type = AlertType.Error;
                _alert.AddMessage(e.d ? e.d : e.responseText);
                _alert.Show();
            },
            complete: function (e) {
                //$('div[id="membros"]').unblock();
            }
        });
    }
    this.Indicar = function (item) {
        this.DataCadastro = new Date();
        var current = this;
        ExecuteMethod(url + "AddUsuario", '{ indicacao: ' + JSON.stringify(this) + '}', function (e) {
            if (e.d) {
                var type = e.d.AlertType;
                if (e.d.AlertType == AlertType.Success) {
                    var date = new Date();
                    var tr = "<tr>" + (hasUserName ? "<td>" + item.Nome + "</td>" : "") + "<td>" + item.Empresa + "</td><td>" + item.Cargo + "</td><td>" + item.Salario +
                    "</td><td>AGUARDANDO APROVAÇÃO</td><td><input class='btn btn-danger' type='button' value='cancelar' onclick='Cancelar(" + item.VagaId + ", " +
                    item.UsuarioIndicadoId + ", { Day: " + date.getDay() +
                        ", Month: " + (date.getMonth() + 1) + ", Year: " + date.getFullYear() + "}, this)'/></td></tr>";
                    $('table[id$="gridVagas"] tr:last').after(tr);
                    $("input[id$='Nome']").val("");
                    $("input[id$='UsuarioId'").val("");
                }
                if (e.d.Show == true && e.d.Messages) {
                    var _alert = new Alert();
                    _alert.Name = "erroGeral";
                    _alert.Type = type;
                    for (var i in e.d.Messages) {
                        _alert.AddMessage(e.d.Messages[i]);
                    }
                    _alert.Show();
                }
            }
        });
    }
    this.Aprovar = function (item) {

        ExecuteMethod(url + "Aprovar", '{ indicacao: ' + JSON.stringify(this) + '}', function (e) {
            if (e.d) {
                var type = e.d.AlertType;
                if (e.d.AlertType == AlertType.Success) {
                    $(item).parent().parent().parent().remove();
                    // $('table[id$="gridVagas"] tr:last').after(tr);
                }
                if (e.d.Show == true && e.d.Messages) {
                    var _alert = new Alert();
                    _alert.Name = "erroGeral";
                    _alert.Type = type;
                    for (var i in e.d.Messages) {
                        _alert.AddMessage(e.d.Messages[i]);
                    }
                    _alert.Show();
                }
            }
        });
    }
    this.Reprovar = function (item) {

        ExecuteMethod(url + "Reprovar", '{ indicacao: ' + JSON.stringify(this) + '}', function (e) {
            if (e.d) {
                var type = e.d.AlertType;
                if (e.d.AlertType == AlertType.Success) {
                    $(item).parent().parent().parent().remove();
                    // $('table[id$="gridVagas"] tr:last').after(tr);
                }
                if (e.d.Show == true && e.d.Messages) {
                    var _alert = new Alert();
                    _alert.Name = "erroGeral";
                    _alert.Type = type;
                    for (var i in e.d.Messages) {
                        _alert.AddMessage(e.d.Messages[i]);
                    }
                    _alert.Show();
                }
            }
        });
    }
    this.Cancelar = function (item) {
        ExecuteMethod(url + "Cancelar", '{ indicacao: ' + JSON.stringify(this) + '}', function (e) {
            if (e.d) {
                var type = e.d.AlertType;
                if (e.d.AlertType == AlertType.Success) {
                    $(item).parent().parent();
                    var element = $(item).parent().parent().parent().children("td").get(hasUserName ? 4 : 3);
                    $(element).html("CANCELADA");
                    $(item).remove();
                }
                if (e.d.Show == true && e.d.Messages) {
                    var _alert = new Alert();
                    _alert.Name = "erroGeral";
                    _alert.Type = type;
                    for (var i in e.d.Messages) {
                        _alert.AddMessage(e.d.Messages[i]);
                    }
                    _alert.Show();
                }
            }
        });
    }
    this.AprovarAluno = function (item, texto) {
        ExecuteMethod(url + "AprovarAluno", '{ indicacao: ' + JSON.stringify(this) + ', feedback: "' + texto + '"}', function (e) {
            if (e.d) {
                var type = e.d.AlertType;
                if (e.d.AlertType == AlertType.Success) {
                    $(item).parent().parent().parent().remove();
                }
                if (e.d.Show == true && e.d.Messages) {
                    var _alert = new Alert();
                    _alert.Name = "erroGeral";
                    _alert.Type = type;
                    for (var i in e.d.Messages) {
                        _alert.AddMessage(e.d.Messages[i]);
                    }
                    _alert.Show();
                }
            }
        });
    }
    this.ReprovarAluno = function (item, texto) {
        ExecuteMethod(url + "ReprovarAluno", '{ indicacao: ' + JSON.stringify(this) + ', feedback: "' + texto + '"}', function (e) {
            if (e.d) {
                var type = e.d.AlertType;
                if (e.d.AlertType == AlertType.Success) {
                    $(item).parent().parent().parent().remove();
                }
                if (e.d.Show == true && e.d.Messages) {
                    var _alert = new Alert();
                    _alert.Name = "erroGeral";
                    _alert.Type = type;
                    for (var i in e.d.Messages) {
                        _alert.AddMessage(e.d.Messages[i]);
                    }
                    _alert.Show();
                }
            }
        });
    }
    return this;
}